let EditorHandler = function () {
    let csrf_token = $("meta[name='csrf-token']").attr("content");
    const {createEditor, createToolbar} = window.wangEditor

    const editorConfig = {
        placeholder: '输入内容...',
        onChange(editor) {
            const html = editor.getHtml()
        },
    }

    editorConfig.MENU_CONF = {}
    editorConfig.MENU_CONF['uploadImage'] = {
        server: '/post/image_upload/',
        fieldName: 'image',
        maxFileSize: 5 * 1024 * 1024,
        maxNumberOfFiles: 10,
        allowedFileTypes: ['image/*'],
        headers: {
            Accept: 'text/x-json',
            "X-CSRFToken": csrf_token,
        },
        withCredentials: true,
        timeout: 5 * 1000,
    }

    const editor = createEditor({
        selector: '#editor-container',
        html: '<p><br></p>',
        config: editorConfig,
        mode: 'default', // or 'simple'
    })
    this.editor = editor;

    const toolbarConfig = {}

    const toolbar = createToolbar({
        editor,
        selector: '#toolbar-container',
        config: toolbarConfig,
        mode: 'default', // or 'simple'
    });
}

EditorHandler.prototype.InitEditPost = function () {
    let content = $("#editor-container").attr("data-post-content");
    this.editor.setHtml(content);
};

EditorHandler.prototype.SubmitEvent = function () {
    let that = this;
    $("#submit-btn").on("click", function (event) {
        event.preventDefault();
        let titleE = $("input[name='title']");
        let title = titleE.val();
        let post_id = titleE.attr("data-post-id");
        let board_id = $("select[name='board_id']").val();
        const content = that.editor.getHtml()
        if (!title) {
            xtalert.alertInfoToast("请输入标题")
            return;
        } else if (!board_id) {
            xtalert.alertInfoToast("请选择板块")
            return;
        } else if (!content) {
            xtalert.alertInfoToast("请输入内容")
            return;
        } else if (!post_id) {
            xtalert.alertInfoToast("请传入帖子id")
            return;
        }
        csrf_ajax.post({
            url: "/settings/post/edit/",
            data: {title, content, board_id, post_id},
            success: function (result) {
                if (result['code'] === 200) {
                    xtalert.alertSuccessToast("修改成功")
                    setTimeout(function () {
                        let data = result['data'];
                        window.location = "/post/detail/" + post_id;
                    }, 1500)
                } else {
                    xtalert.alertErrorToast(result['message']);
                }
            },
            fail: function () {
                xtalert.alertNetworkError();
            }
        });
    });
};

EditorHandler.prototype.CancelEditPost = function () {
    $("#cancel-edit-btn").click(function (event) {
        event.preventDefault();
        // 添加确认提示
        xtalert.alertConfirm({
            "msg": "确定要取消编辑吗？未保存的更改将会丢失",
            "confirmCallback": function () {
                window.location.href = "/settings/post/manage/";
            }
        });
    });
};


EditorHandler.prototype.run = function () {
    this.InitEditPost();
    this.SubmitEvent();
    this.CancelEditPost();
}

$(function () {
    let editorHandler = new EditorHandler();
    editorHandler.run();
})